package ysm.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ysm.entity.Graduates;
import ysm.entity.Profession;
import ysm.mapper.GraduatesMapper;
import ysm.mapper.ProfessionMapper;
import ysm.tools.ReadExcel;

@Service
public class GraduatesService {

	@Autowired
	private GraduatesMapper graduatesMapper;
	
	@Autowired
	private ProfessionMapper professionMapper;
	
	@Autowired
	private ReadExcel readExcel;
	
	public List<Graduates> loadAllGraduates() {
		// ????
		List list = graduatesMapper.loadAllGraduates();
		List result = new ArrayList();
//		for (int i = 0; i < list.size(); i++) {
//			Graduates graduates = (Graduates) list.get(i);
//			Profession profession = professionMapper.selectByPrimaryKey(graduates.getProfession());
//			graduates.setProfessionname(profession.getProfessionname());
//			result.add(graduates);
//		}
		for(int i = 0; i < list.size(); i++) {
			Graduates graduates = (Graduates) list.get(i);
			Profession profession = professionMapper.selectByPrimaryKey(graduates.getProfession());
			graduates.setProfessionname(profession.getProfessionname());
			result.add(graduates);
		}
		return result;
	}
	
	public void deleteByPrimaryKey(Integer id) {
		graduatesMapper.deleteByPrimaryKey(id);
	}

	public void insert(Graduates record) {
		graduatesMapper.insert(record);
	}

	public void insertSelective(Graduates record) {
		graduatesMapper.insertSelective(record);
	}

	public Graduates selectByPrimaryKey(Integer id) {
		Graduates graduates = graduatesMapper.selectByPrimaryKey(id);
		Profession profession = professionMapper.selectByPrimaryKey(graduates.getProfession());
		graduates.setProfessionname(profession.getProfessionname());
		return graduates;
	}

	public void updateByPrimaryKeySelective(Graduates record) {
		graduatesMapper.updateByPrimaryKeySelective(record);
	}

	public void updateByPrimaryKey(Graduates record) {
		graduatesMapper.updateByPrimaryKey(record);
	}
	
	public void importMuchGraduateData(String filePath) {
		try {
			List list = readExcel.read(filePath);
			for(Object object : list) {
				Graduates graduates = (Graduates) object;
				graduatesMapper.insert(graduates);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
